﻿#region Copyright (c) 11/24/2011, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Globalization;
using System.Windows.Controls;
using NUnit.Framework;


namespace Amarok.Presentation
{
	[TestFixture]
	public class Test_ValidationRule
	{
		private class MyValidationRule : ValidationRule<String>
		{
			public String Value;
			public CultureInfo CultureInfo;

			protected override ValidationResult OnValidate(String value, CultureInfo cultureInfo)
			{
				this.Value = value;
				this.CultureInfo = cultureInfo;

				if (value == null && cultureInfo == null)
					throw new ApplicationException("TEST");

				return new ValidationResult(false, "ERROR");
			}

			public ValidationResult TestSuccess()
			{
				return base.Success();
			}

			public ValidationResult TestFailure(String message, params Object[] arguments)
			{
				return base.Failure(message, arguments);
			}
		}


		// --- TESTS ---


		[Test]
		public void Test_Validate()
		{
			var rule = new MyValidationRule();

			Assert.That(rule.Value, Is.Null);
			Assert.That(rule.CultureInfo, Is.Null);

			var result = rule.Validate("BLAH", CultureInfo.InvariantCulture);

			Assert.That(rule.Value, Is.EqualTo("BLAH"));
			Assert.That(rule.CultureInfo, Is.EqualTo(CultureInfo.InvariantCulture));

			Assert.That(result.IsValid, Is.False);
			Assert.That(result.ErrorContent, Is.EqualTo("ERROR"));
		}

		[Test]
		public void Test_Validate_TypeMismatch()
		{
			var rule = new MyValidationRule();

			Assert.That(rule.Value, Is.Null);
			Assert.That(rule.CultureInfo, Is.Null);

			Assert.Throws<InvalidCastException>(() =>
				{
					rule.Validate(123, CultureInfo.InvariantCulture);
				});

			Assert.That(rule.Value, Is.Null);
			Assert.That(rule.CultureInfo, Is.Null);
		}

		[Test]
		public void Test_Validate_Exception()
		{
			var rule = new MyValidationRule();

			Assert.That(rule.Value, Is.Null);
			Assert.That(rule.CultureInfo, Is.Null);

			Assert.Throws<ApplicationException>(() =>
				{
					rule.Validate(null, null);
				});

			Assert.That(rule.Value, Is.Null);
			Assert.That(rule.CultureInfo, Is.Null);
		}

		[Test]
		public void Test_Success()
		{
			var rule = new MyValidationRule();
			var result = rule.TestSuccess();

			Assert.That(result.IsValid, Is.True);
			Assert.That(result.ErrorContent, Is.Null);

			var result2 = rule.TestSuccess();

			Assert.That(result2, Is.SameAs(result));
		}

		[Test]
		public void Test_Failure()
		{
			var rule = new MyValidationRule();
			var result = rule.TestFailure("ERROR:{0}_{1}", 1234, "IoFailure");

			Assert.That(result.IsValid, Is.False);
			Assert.That(result.ErrorContent, Is.EqualTo("ERROR:1234_IoFailure"));
		}

	}
}

#endif
